package logic

import (
	"context"
	"shuqi-novel/model/mysql"
	"strconv"

	"shuqi-novel/userrpc/internal/svc"
	"shuqi-novel/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 用户登录
func (l *LoginLogic) Login(in *userrpc.LoginRequest) (*userrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	err := user.FindUserByUsername(mysql.DB, in.Username)
	if err != nil {
		return nil, err
	}

	return &userrpc.LoginResponse{
		UserId: strconv.Itoa(int(user.ID)),
	}, nil
}
